package com.mvp.mvpdemo.base


import android.app.Dialog
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.mvp.base.BaseActivity
import com.mvp.mvpdemo.R


/**
 * Created By Mahongyin
 * Date    2020/10/15 16:19
 *
 */
abstract class BaseMvpActivity<V : IBaseView, P : IPresenter<V>> : BaseActivity(), IBaseView {
    /**
     * 持有表示层
     */
    protected var mPresenter: P? = null

    protected abstract fun createPresenter(): P

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mPresenter = createPresenter()
        mPresenter?.attachView(this as V)
        //P 层注册生命周期监听
//        mPresenter?.let { lifecycle.addObserver(it) }
    }


    private var alertDialog: Dialog? = null
    override fun showLoading() {
        if (alertDialog == null) {
            alertDialog = Dialog(this);
            val layout = View.inflate(this, R.layout.progress, null)
            alertDialog?.apply {
                setContentView(layout)
                    //点击Dialog外部消失
                setCanceledOnTouchOutside(false)
                //禁用返回键
                setCancelable(false)
            }
        }
        if (alertDialog?.isShowing() ?: true) {
            return
        } else {
            alertDialog?.show()
        }
    }

    override fun onDestroy() {
        alertDialog?.dismiss()
        alertDialog=null
        super.onDestroy()
    }
    override fun hideLoading() {
        if (alertDialog?.isShowing ?: false) {
            alertDialog?.dismiss()
        }

    }

    override fun showShortToast(msg: CharSequence) {
        ToastUtils.showShort(msg)
    }

    override fun showLongToast(msg: CharSequence) {
        ToastUtils.showLong(msg)
    }
}